package cqupt.spark_log.info;

import java.io.IOException;
import java.util.HashMap;

import cqupt.spark_log.entity.StrategyTable;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class NodeEnergyInfo {
	WritableWorkbook book = null;

	public NodeEnergyInfo(WritableWorkbook book) {
		this.book = book;
	}

	public void save(String path, String index) {
		try {
			StrategyTable strategyTable = new StrategyTable(path);
			// 初始化表
			initable(strategyTable);
			// 写数据
			writeBook(strategyTable, index);
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 写数据
	 * 
	 * @param strategyTable
	 * @param index
	 * @throws WriteException
	 * @throws RowsExceededException
	 */
	private void writeBook(StrategyTable strategyTable, String index) throws RowsExceededException, WriteException {
		WritableSheet sheet = book.getSheet(0);
		Label label = new Label(0, Integer.parseInt(index), index);
		sheet.addCell(label);
		// 获取节点列表
		String[] nodeList = strategyTable.getNodeList();
		// 获取所有的阶段
		String[] stagelist = strategyTable.getStageList();
		for (int i = 0; i < stagelist.length; i++) {
			HashMap<String, HashMap<String, String[]>> stageInfo = strategyTable.getStageInfoByStageId(stagelist[i]);
			HashMap<String, Double> sum = new HashMap<String, Double>();
			for(String key:stageInfo.keySet()){
				 HashMap<String, String[]> value = stageInfo.get(key);
				 for (int j=0;j<nodeList.length;j++) {
					 String host = nodeList[j];
					 String[] energyRunTime = value.get(host);
					 Double energy = Double.valueOf(energyRunTime[0]);
					 if (sum.containsKey(host)) {
						 sum.put(host, sum.get(host)+energy);
					 } else {
						 sum.put(host, energy);
					 }
				 }
			}
			for (int j=0,k=0;j<nodeList.length;j++,k+=stagelist.length) {
				Label sumValue = new Label(k+i+1, Integer.parseInt(index), sum.get(nodeList[j])+"");
				sheet.addCell(sumValue);
			}
		}
	}

	private void initable(StrategyTable strategyTable) throws RowsExceededException, WriteException, IOException {
		// 先判断是否已经初始化好了
		String[] sheetName = book.getSheetNames();
		WritableSheet sheet = null;
		if (sheetName.length == 0) {
			sheet = book.createSheet("sheet1", 0);
		} else {
			sheet = book.getSheet(0);
		}
		if (sheet.getRows() > 0) {
			return;
		}
		// 获取节点列表
		String[] nodeList = strategyTable.getNodeList();
		// 获取所有的阶段
		String[] stagelist = strategyTable.getStageList();
		// 第一行初始化为节点标志
		for (int i = 0, j = 0; j < nodeList.length; i += stagelist.length, j++) {
			Label label = new Label(i + 1, 0, nodeList[j]);
			sheet.addCell(label);
		}
		// 第二行初始化阶段
		for (int i = 0; i < nodeList.length * stagelist.length; i += stagelist.length) {
			for (int j = 0; j < stagelist.length; j++) {
				Label label = new Label(i + j + 1, 1, stagelist[j]);
				sheet.addCell(label);
			}
		}
	}

	public void writeAndclose() {
		try {
			book.write();
			book.close();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
